﻿using System.Diagnostics;
using System.Collections.Generic;
using Microsoft.Practices.EnterpriseLibrary.Logging;

namespace Utility.Logging
{
    /// <summary>
    /// Simple class to simplyf logging using the enterprise library
    /// </summary>
    public static class Log
    {
        public static void LogWarning(string message)
        {
            Logger.Write(message, "Warning");
        }

        public static void LogInfo(string message)
        {
            Logger.Write(message, "Information");
        }

        public static void LogDebug(string message)
        {
            Logger.Write(message, "Debug");
        }

        /// <summary>
        /// Generic Error logging - dependant on "Error" category source in app config or progrmatic setting
        /// </summary>
        /// <param name="e">The exception to pass</param>
        public static void LogError(System.Exception e)
        {
            string innerException;
            IDictionary<string, object> dictionary = new Dictionary<string, object>();
            dictionary.Add("Stack Trace", e.StackTrace);
            if (e.InnerException != null)
            {
                innerException = e.InnerException.Message;
                dictionary.Add("Inner Exception", innerException);
            }
            LogOutput(e.Message, "Error", 1, 1, TraceEventType.Error, e.Source, dictionary);
        }

        public static void LogEvent(string message, string category, int priority, int eventId, TraceEventType severity, string title, IDictionary<string, object> properties)
        {
            LogOutput(message, category, priority, eventId, severity, title, properties);
        }

        private static void LogOutput(string message, string category, int priority, int eventId, TraceEventType severity, string title, IDictionary<string, object> properties)
        {

            LogEntry logEntry = new LogEntry();
            logEntry.Title = title;
            logEntry.Categories.Clear();
            logEntry.Categories.Add(category);
            logEntry.Priority = priority;
            logEntry.EventId = eventId;
            logEntry.Severity = severity;
            logEntry.Message = message;
            if (properties.Count > 0)
            {
                logEntry.ExtendedProperties = properties;
            }

            Logger.Write(logEntry);
        }
    }
}
